package fleporcq.simpleReflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class ClassDescriptor {

    protected Class clazz;

    public ClassDescriptor(Class clazz) {
        this.clazz = clazz;
    }

    public String getName() {
        return this.clazz.getSimpleName();
    }

    public boolean isAbstract() {
        int modifiers = this.clazz.getModifiers();
        return Modifier.isAbstract(modifiers);
    }

    public boolean isInterface() {
        int modifiers = this.clazz.getModifiers();
        return Modifier.isInterface(modifiers);
    }

    protected List<ClassDescriptor> getInterfaceDescriptors() {
        List<ClassDescriptor> classDescriptors = new ArrayList<ClassDescriptor>();
        for (Class clazz : this.clazz.getInterfaces()) {
            classDescriptors.add(new ClassDescriptor(clazz));
        }
        return classDescriptors;
    }


    public List<FieldDescriptor> getFieldDescriptors() {
        List<FieldDescriptor> fieldDescriptors = new ArrayList<FieldDescriptor>();
        for (Field field : this.clazz.getDeclaredFields()) {
            fieldDescriptors.add(new FieldDescriptor(field));
        }
        return fieldDescriptors;
    }

    public List<MethodDescriptor> getMethodDescriptors() {
        List<MethodDescriptor> methodDescriptors = new ArrayList<MethodDescriptor>();
        for (Method method : this.clazz.getDeclaredMethods()) {
            methodDescriptors.add(new MethodDescriptor(method));
        }
        return methodDescriptors;
    }

    public ClassDescriptor getSuperDescriptor() {
        return new ClassDescriptor(this.clazz.getSuperclass());
    }

    public boolean hasSuperClass() {
        return this.clazz.getSuperclass() != Object.class;
    }

    @Override
    public String toString() {
        return this.getName();
    }

}
